package net.service.Impl;

import net.common.ServiceResponse;
import net.mapper.CartMapper;
import net.mapper.ProductMapper;
import net.pojo.Cart;
import net.pojo.Product;
import net.pojo.User;
import net.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/18 9:15
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;
    @Override
    public List<Cart> findByUid(Integer uid) {
        List<Cart> list = cartMapper.findByUid(uid);
        for (Cart cart : list){
            Product product = productMapper.findById(cart.getPid());
            cart.setProduct(product);
        }
        return list;
    }

    @Override
    public int addCart(Cart cart) {
        cart.setQuantity(1);
        cart.setChecked(0);
        //查询当前用户的购物车情况
        Cart productByCart = cartMapper.findProductByCart(cart.getPid(), cart.getUid());
        if (productByCart != null){
            cart.setQuantity(cart.getQuantity()+productByCart.getQuantity());
            return cartMapper.updateQuantity(cart);
        }
        //不存在时处理
        cart.setCreate_time(new Date());
        return cartMapper.addCart(cart);
    }

    @Override
    public int updateQuantity(Cart cart) {
        return cartMapper.updateQuantity(cart);
    }

    @Override
    public int deleteById(String pid) {
        return cartMapper.deleteById(pid);
    }

    @Override
    public int deleteCartByUid(int id) {
            return cartMapper.deleteCartByUid(id);
    }
}
